
using UnityEngine;

//地形工厂
[CreateAssetMenu]
public class GameTileContentFactory : GameObjectFactory
{

    //添加两个预制配置字段
    [SerializeField]
    GameTileContent destinationPrefab = default;//目的地
    [SerializeField]
    GameTileContent emptyPrefab = default;      //空地
    [SerializeField]
    GameTileContent wallPrefab = default;       //墙
    [SerializeField]
    GameTileContent spawnPointPrefab = default; //怪物出生点
    [SerializeField]
    Tower[] towerPrefabs = default;      //塔
    

    public void Reclaim(GameTileContent content)
    {
        Debug.Assert(content.OriginFactory == this,"Wrong factory reclaimed!");
        Destroy(content.gameObject);//销毁 GameObject、组件或资源。
    }


    T Get<T>(T prefab) where T : GameTileContent
    {
        T instance = CreateGameObjectInstance(prefab);//实例化预制体
        instance.OriginFactory = this;//设置其原始工厂
        return instance;
    }

    //获取相应预制体的实例
    public GameTileContent Get(GameTileContentType type)
    {
        switch(type)
        {
            case GameTileContentType.Destination :
                return Get(destinationPrefab);
            case GameTileContentType.Empty:
                return Get(emptyPrefab);
            case GameTileContentType.Wall:
                return Get(wallPrefab);
            case GameTileContentType.SpawnPoint:
                return Get(spawnPointPrefab);
        }
        Debug.Assert(false, "Unsupported non-tower type: " + type);
        return null;
    }

    public Tower Get(TowerType type)
    {
        Debug.Assert((int) type < towerPrefabs.Length, "Unsupported tower type!");
        Tower prefab = towerPrefabs[(int) type];
        Debug.Assert(type == prefab.TowerType, "Tower prefab at wrong index!");
        return Get(prefab);
    }
}